package com.zuipin.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zuipin.entity.MemberAccount;
import com.zuipin.entity.MemberLevel;
import com.zuipin.mapper.MemberLevelMapper;
import com.zuipin.service.IMemberAccountService;
import com.zuipin.service.IMemberLevelService;
import com.zuipin.util.ConstantsUtil;
import com.zuipin.util.DateUtils;
import com.zuipin.util.StringUtil;
import com.zuipin.vo.EnumVo;

/**
 * @ClassName: MemberLevelServiceImpl
 * @author wuyicheng
 * @time 2017年3月27日下午4:02:07
 */
@Service
public class MemberLevelServiceImpl implements IMemberLevelService {
	
	@Resource
	private MemberLevelMapper		memberLevelMapper;
	@Resource
	private IMemberAccountService	memberAccountServeice;
	
	/**
	 * @Title: findById
	 * @author: wuyicheng
	 * @date: 2017年3月27日下午5:28:22
	 * @param id
	 * @return
	 * @throws
	 */
	@Override
	public MemberLevel findById(Long id) {
		return memberLevelMapper.findById(id);
	}
	
	/**
	 * @Title: findList
	 * @author: wuyicheng
	 * @date: 2017年3月27日下午5:28:22
	 * @param memberLevel
	 * @return
	 * @throws
	 */
	@Override
	public List<MemberLevel> findList(MemberLevel memberLevel) {
		List<MemberLevel> list = memberLevelMapper.findLevelList(memberLevel);
		return list;
	}
	
	/**
	 * @Title: insert
	 * @author: wuyicheng
	 * @date: 2017年3月27日下午5:28:22
	 * @param memberLevel
	 * @throws
	 */
	@Override
	public void insert(MemberLevel memberLevel) {
		memberLevel.setCreateTime(DateUtils.getCurrentDateTime());
		memberLevel.setUpdateTime(DateUtils.getCurrentDateTime());
		memberLevel.setDelFlag(ConstantsUtil.DEL_FLAG_NO_0);
		memberLevelMapper.insert(memberLevel);
	}
	
	/**
	 * @Title: update
	 * @author: wuyicheng
	 * @date: 2017年3月27日下午5:28:22
	 * @param memberLevel
	 * @throws
	 */
	@Override
	public void update(MemberLevel memberLevel) {
		memberLevel.setUpdateTime(DateUtils.getCurrentDateTime());
		memberLevelMapper.update(memberLevel);
		
	}
	
	/**
	 * @Title: deleteById
	 * @author: wuyicheng
	 * @date: 2017年3月27日下午5:28:22
	 * @param id
	 * @throws
	 */
	@Override
	public void deleteById(Long id) {
		MemberLevel memberLevel = new MemberLevel();
		memberLevel.setDelFlag(ConstantsUtil.DEL_FLAG_YES_1);
		update(memberLevel);
	}
	
	@Override
	public MemberLevel findByMemberIdAndSysId(Long memberId, Long sysId) {
		MemberAccount memberAccount = memberAccountServeice.findByMemberIdAndSysId(memberId, sysId);
		if (memberAccount != null) {
			return memberLevelMapper.findById(memberAccount.getMemberLevelId());
		}
		return null;
	}
	
	@Override
	public MemberLevel findByLeverAndSysId(String lever, Long sysId) {
		if (StringUtil.isNotBlank(lever) && sysId != null) {
			MemberLevel memberLevel = new MemberLevel();
			memberLevel.setLever(lever);
			memberLevel.setSysId(sysId);
			return memberLevelMapper.findByLeverAndSysId(memberLevel);
		}
		return null;
	}
	
	/** 
	 *
	 * @Title: findByGrowthValue 
	 * @author: wuyicheng
	 * @date: 2017年6月19日下午2:45:20 
	 * @param sysId
	 * @param totalGrowthValue
	 * @throws 
	 */
	@Override
	public MemberLevel findByGrowthValue(Long sysId, Integer totalGrowthValue) {
		return memberLevelMapper.findByGrowthValue(sysId, totalGrowthValue);
		
	}
	
	/**
	 * 查询会员等级的EnumVo
	 */
	@Override
	public List<EnumVo> findLevelListEnum(Long sysId) {
		MemberLevel level = new MemberLevel();
		level.setSysId(sysId);
		List<MemberLevel> levels = findList(level);
		List<EnumVo> list = new ArrayList<EnumVo>();
		for (MemberLevel m : levels) {
			EnumVo vo = new EnumVo();
			vo.setIndex(Integer.valueOf(m.getId() + ""));
			vo.setName(m.getMemberLevelName());
			vo.setValue(m.getLever());
			list.add(vo);
		}
		return list;
	}
	
}
